*This file cleans files to execute the Shift-share robustness Adao et al. (2019).

global municontrols  agrishare_1940  town city manushare_1940 

cd "$temp"

use "$temp\Long_shares2.dta", clear // Here municipality codes in strings
drop if ind39>91
rename share share_
reshape wide share_, i(mun39) j(ind39) 
save wide_shares, replace

*this file makes data with equal dimensions 
use "$temp\bartik_50_c.dta", clear

drop if laani50==.
drop if manushare_1940==.
drop if agrishare_1940==.

save "$temp\bartik_50_c_sample.dta", replace

joinby mun39 using "wide_shares", unmatched(master)

drop agrishare_1940-_merge
foreach x of varlist share_1-share_91{
replace `x'=0 if `x'==.
}

save "$temp\wide_shares_sample.dta", replace

use "$temp\bartik_50_c_agri.dta", clear

drop if laani50==.
drop if manushare_1940==.
drop if agrishare_1940==.

save "$temp\bartik_50_c_agri_sample.dta", replace

reg primary50_individual bartik_standardized manushare_1940 $municontrols i.laani50 [aw=wei], r

joinby mun39 using "wide_shares", unmatched(master)

drop agrishare_1940-_merge

foreach x of varlist share_1-share_91{
replace `x'=0 if `x'==.
}

save "$temp\wide_shares_sample_agri.dta", replace

use bartik_70_c_agri,clear
gen ln_svatv=ln(svatv) 

drop if ln_svatv==.
drop if laani50==.
drop if manushare_1940==.
drop if agrishare_1940==.

save "$temp\bartik_70_c_agri_sample.dta", replace

joinby mun39 using "wide_shares", unmatched(master)

drop agrishare_1940-_merge

foreach x of varlist share_1-share_91{
replace `x'=0 if `x'==.
}
save "$temp\wide_shares_sample_agri_70.dta", replace

use bartik_70_c,clear
gen ln_svatv=ln(svatv) 

drop if ln_svatv==.
drop if laani50==.
drop if manushare_1940==.
drop if agrishare_1940==.

save "$temp\bartik_70_c_sample.dta", replace

joinby mun39 using "wide_shares", unmatched(master)

drop agrishare_1940-_merge

foreach x of varlist share_1-share_91{
replace `x'=0 if `x'==.
}
save "$temp\wide_shares_sample_70.dta", replace

////////////////////////////////////////
*Upward mobility young
//////////////////////////////////////

use "$temp\bartik_young", clear

drop if laani50==.
drop if manushare_1940==.
drop if agrishare_1940==.

reg yos bartik_standardized manushare_1940 $municontrols i.laani50 [aw=wei], r

save "$temp\bartik_young_sample.dta", replace


joinby mun39 using "wide_shares", unmatched(master)

drop agrishare_1940-_merge

foreach x of varlist share_1-share_91{
replace `x'=0 if `x'==.
}
save "$temp\wide_shares_sample_young.dta", replace

use "$temp\bartik_young_UM", clear

drop if laani50==.
drop if manushare_1940==.
drop if agrishare_1940==.

save "$temp\bartik_young_UM_sample.dta", replace

joinby mun39 using "wide_shares", unmatched(master)

drop agrishare_1940-_merge

foreach x of varlist share_1-share_91{
replace `x'=0 if `x'==.
}
save "$temp\wide_shares_sample_UM.dta", replace


